<template>
	<div>
		<!--flag为true表示常规商品    flag 为false为大件商品-->
		<confirm-getTime :data1 = "normalTime"   idName="name1" :flag= true v-if="checkedCartListNormal.length > 0"></confirm-getTime>
		<confirm-order-list :checkedCartList="checkedCartListNormal" :flag= true v-if="checkedCartListNormal.length > 0"></confirm-order-list>
		<confirm-getTime :data1 = "bigTime"  idName="name2" :flag= false v-if="checkedCartListBig.length > 0"></confirm-getTime>
		<confirm-order-list :checkedCartList="checkedCartListBig" :flag= false v-if="checkedCartListBig.length > 0"></confirm-order-list>
		<confirm-order-detail :sendOrder="sendOrder" :checkedCartList="checkedCartList"></confirm-order-detail>
	</div>
</template>

<script>
	import {Toast} from '../../../utils/toast';
	import axios from 'axios';
	import ConfirmGetTime from "../../../real-components/ConfirmOrder/ConfirmGetTime";
	import ConfirmOrderList from "../../../real-components/ConfirmOrder/ConfirmOrderList";
	import ConfirmOrderDetail from "../../../real-components/ConfirmOrder/ConfirmOrderDetail";
	import {getPageTree, initDomTree} from "../../../utils/getPageTree";
	import {mapGetters,mapActions,mapMutations} from "vuex";
	export default {
		props: {
           
        },
        computed:{
        	...mapGetters('wxh',[
				'sessionId',
				'userInfo'
			]),
			...mapGetters('zx',[
				'getAddressDetail','getMynode',"getTimeNormal","getTimeBig"
			]),
			normalTime(){
				return this.normalTimeList;
			},
			bigTime(){
				return this.bigTimeList
			},
        	pageTree () {
				return getPageTree(this, this.page);
			}
        },
        data () {
        	// distributiontype 0 表示常规商品  1 表示大件商品
            return {

                checkedCartList:[],   //购物车所有商品
                checkedCartListNormal:[],   //用于存放常规商品的数组
                checkedCartListBig:[],  //用于存放大件商品的数组
                maxTimeNormal:[],   //用于请求时间列表接口的参数
                maxTimeBig:[],    //用于请求时间列表接口的参数
                normalTimeList:[], //用于接收常规商品时间列表请求结果
                bigTimeList:[],   //用于接收大件商品时间列表请求结果
                shopCartIds:[],      //用于下单的参数
                address:null,
            }
        },
        watch:{
//      	normalTimeList(newValue,oldValue){
//      		return newValue;
//      	},
//      	bigTimeList(newValue,oldValue){
//      		return newValue;
//      	}
        },
        methods: {
        	...mapMutations('cjy',[
        		"changeCheckedCartList"
        	]),
        	...mapMutations('zx',[
        		"setTimeNormal","setTimeBig"
        	]),
        	//获取最大时间
        	getMaxTime(arr){
        		console.log(arr,"常规 大件”");
        		if(arr.length > 0){
        			var max1 = arr[0].distributionpreparationtime;
	        		var max2 = arr[0].distributionprocessingtime;
	        		var myArr = [];
	        		for(let i = 0; i < arr.length; i++){
	        			if(max1 < arr[i].distributionpreparationtime){
	        				max1 = arr[i].distributionpreparationtime;
	        			}
	        			if(max2 < arr[i].distributionprocessingtime){
	        				max2 = arr[i].distributionprocessingtime;
	        			}
	        		}
	        		myArr.push(max1);
	        		myArr.push(max2);
	        		
	        		return myArr;
        		}
        	},
        	//获取初始数据
        	cartList(){
        		//获取所有购物车商品
        		this.checkedCartList = this.$store.state.cjy.checkedCartList;
        		
        		//区分大件商品和常规商品  0 常规   1 大件
				for(var i = 0; i < this.checkedCartList.length; i++){
					var obj = {}
					obj['rid'] = this.checkedCartList[i].rid;
					this.shopCartIds.push(obj);
					
					if(this.checkedCartList[i].distributiontype === 0 || this.checkedCartList[i].distributiontype === null){
						this.checkedCartListNormal.push(this.checkedCartList[i]);
					}else if(this.checkedCartList[i].distributiontype === 1){
						this.checkedCartListBig.push(this.checkedCartList[i]);
					}
				}
				

				//获取到常规和大件的加工时间 准备时间
				this.maxTimeNormal = this.getMaxTime(this.checkedCartList);
				this.maxTimeBig = this.getMaxTime(this.checkedCartListBig);
				
				
				//请求昌松的可选时间接口   获取时间列表
				if(this.checkedCartListNormal.length > 0){
					this.getTimeList(this.maxTimeNormal,0);
				}
				if(this.checkedCartListBig.length > 0){
					this.getTimeList(this.maxTimeBig,1);
				}
				
				
				
				//不同地方的返回
				let arr = this.userInfo.addressList;
				let query = this.$route.query.name;
				if(query){
					if(!query.flag){
						for(var j = 0; j < this.userInfo.addressList.length; j++ ){
		        			if(this.userInfo.addressList[j].defaultaddress === '默认'){
		        				this.address = this.userInfo.addressList[j];
		        				this.addressId = this.userInfo.addressList[j]
		        			}
		        		}
					}else{
						this.address = query;
					}
				}else{
					for(var j = 0; j < this.userInfo.addressList.length; j++ ){
	        			if(this.userInfo.addressList[j].defaultaddress === '默认'){
	        				this.address = this.userInfo.addressList[j];
	        				console.log(this.address,'直接进来')
	        				this.addressId = this.userInfo.addressList[j];
	        			}
	        		}
				}
				
				console.log(this.address,'收货地址信息')
        	},
        	//获取时间列表方法
        	getTimeList(data,type){
        		console.log(data,type)
        		let params = {
        			readyTm:data[0],
        			machineTm:data[1],
					storeId:localStorage.getItem('storeCode')
        		};
        		
//      		window.api.local_host
//        		'http://172.19.5.196:8112'
        		let config = {
	            	url:window.api.local_host+"/delivery_config/tm_interval/"+type,
	            	method: 'GET',
	            	params,
	            	headers: {'sessionId': this.sessionId},
	           };
	           console.log(config.url)
	           axios(config).then((res)=>{
	           		console.log(res.data.data,'shiajianliebaios',type);
	           		if(type === 0){
	           			
	           			this.normalTimeList = res.data.data;
	           			//初始化store中的时间
	           			this.setTimeNormal(res.data.data[0].deliveryList[0])
//	           			console.log(res.data.data,'获取到jieguo');
//	           			console.log(this.normalTimeList,'获取到了normal')
	           		}else if(type === 1){
	           			
	           			this.bigTimeList = res.data.data;
//	           			console.log(res.data.data,'获取到jieguo');
	           			//初始化store中的时间
	           			this.setTimeBig(res.data.data[0].deliveryList[0])
//	           			console.log(res.data.data,'获取到了big')
//	           			console.log(this.bigTimeList,'获取到了big')
	           		}
	           })
        	},
        	sendOrder(){
 				
  				if(!this.sessionId){
  					this.$router.push('login');
  				}
  				if(!this.address){
  					Toast({
		                message: '请选择收货地址',
		                duration: 1000,
		                type:'fail'
		            });
  				}
  				
  				var orderDeliveryTms  = [];
  				var norObj = {};
  				var bigObj = {};
  				var norMalTime = this.getTimeNormal;
  				var bigTime = this.getTimeBig;
//				console.log(norMalTime,'去下单罗');
  				
				if(norMalTime){
					norObj.expectedTmStart = norMalTime.startBookTm;
					norObj.expectedTmEnd = norMalTime.endBookTm;
					norObj.goodsDeliveryType = 0;
				}
				if(bigTime){
					bigObj.expectedTmStart = bigTime.startBookTm;
					bigObj.expectedTmEnd = bigTime.endBookTm;
					bigObj.goodsDeliveryType = 1;
				}
			
//				console.log(norMalTime,bigTime,'8888')
				if(this.checkedCartListNormal.length != 0){
					console.log(456)
					orderDeliveryTms.push(norObj);
				}
				if(this.checkedCartListBig.length != 0){
					console.log(123)
					orderDeliveryTms.push(bigObj);
				}
				
        		let params = {
        			addressId:this.address.vipdtlid,
        			cartId:this.checkedCartList[0].cartId,
        			logisticsType:0,
        			note:this.getMynode,
        			shopCartIds:this.shopCartIds,
        			shopId:localStorage.getItem('storeId'),
        			orderDeliveryTms
        		};
        		let query = this.$route.query.name;
      			if(query){
        			if(query.flag){
        				params.deliveryAddress = this.address.result;
        			}else{
        				params.deliveryAddress = this.getAddressDetail;
        			}
      				
      			}else{
      				params.deliveryAddress = this.getAddressDetail;
      			}
//    			172.19.5.193:8112 

        		let config = {
	            	url: 'http://172.19.5.193:8112/order/insertDto',
	            	method: 'POST',
	            	data: params,
	            	headers: {'sessionId': this.sessionId, 'Content-Type':'application/json', 'Accept' : 'application/json'},
	            }
        		
        		if(this.address && orderDeliveryTms.length > 0){
	        			axios(config).then((data)=>{
	        			console.log(data.data.returncode,'returncode');
	        			if(data.data.returncode == 1){
	        				console.log('订单生成');
	        				var arr = [];
	        				this.changeCheckedCartList(arr);
	        				this.$store.dispatch('cjy/queryCart');
	        				
	        				var orderNo = data.data.data[0];
	//			            commonObject.confirm_order = {'orderNo': orderNo, 'realPay': submitInfor.allPrice.replace('￥','')}
				            var u = navigator.userAgent;
	
				            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
				            var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
				            if (isAndroid) {
								this.$router.push('apay/'+orderNo);
				            }else if (isIOS) {
				            	this.$router.push('ipay/'+orderNo);
	//			            	console.log('苹果');
				            }else{
				            	this.$router.push('apay/'+orderNo);
				            }
				            
	        			}else{
	        				console.log('失败');
	        			}
	        		})
        		}
        	}
        },
        components: {
	        "confirm-order-detail": ConfirmOrderDetail,
	        "confirm-getTime": ConfirmGetTime,
	        "confirm-order-list": ConfirmOrderList
	        
	    },
	    beforeDestroy(){
	    	this.setTimeNormal({});
	    	this.setTimeBig({});
	    },
	    created(){
	    	this.cartList();
	    	//console.log(this.cartList,'this.cartList');
//      	this.getDispatchTime(1508467589017,1508474825845,15);
	    }
	}
</script>

<style>
</style>